****************************************************************************************************************************
Date: 02 MAR 2015

Purpose: Add death date for children and parents

****************************************************************************************************************************;

*****************************************************
*Indhenter min sample
*****************************************************;
libname in 'K:\Data\Workdata\702727\xru\NICU project\Datasæt';

data nicu;
set in.nicu_parentalhealth (keep=pnr pnrm pnrf fodtdato birthyear);
run;


*****************************************************
*Tilføjer om barnet er registreet med en dødsdato
*****************************************************;

libname in1 'F:\Rawdata\702727';

data dode;
set in1.dode2011 (rename=(DODDATO=deathdate));

run;

data dode;
set dode;
label deathdate="Date of death";

keep pnr deathdate;  
run;

*Sorterer de to datasæt jeg vil merge sammen;
proc sort data=nicu;
by pnr;
run;

proc sort data=dode;
by pnr;
run;

*Merger dødsdato på;
data dod_dato;

merge nicu (in=a) dode;
by pnr;
if a;
run;


*****************************************************************
Tilføjer om forældrene er registreet med en dødsdato
***************************************************************;

*Gør det først for mor;

data dode_mor;
set dode (rename=(deathdate=deathdate_mom pnr=pnrm));
run;

data dode_mor;
set dode_mor;
label pnrm="Personnummer - mom";
label deathdate_mom="Date of death - mom";
run;
*Sorterer de to datasæt jeg vil merge sammen;
proc sort data=dod_dato;
by pnrm;
run;
proc sort data=dode_mor;
by pnrm;
run;

*Merger dødsdato på;
data dod_dato_mor;
merge dod_dato (in=a) dode_mor;
by pnrm;
if a;

run;

*************************
Gør tilsvarende for far
*************************;

data dode_far;
set dode (rename=(deathdate=deathdate_dad pnr=pnrf));
run;

data dode_far;
set dode_far;
label pnrf="Personnummer - dad";
label deathdate_dad="Date of death - dad";
run;
*Sorterer de to datasæt jeg vil merge sammen;
proc sort data=dod_dato_mor;
by pnrf;
run;
proc sort data=dode_far;
by pnrf;
run;

*Merger dødsdato på;
data dod_dato_dad;
merge dod_dato_mor (in=a) dode_far;
by pnrf;
if a;

run;

proc sort data=dod_dato_dad;
by pnrm birthyear;
run;


data death_dates;
set dod_dato_dad;
drop FODTDATO birthyear;
run;


*STATA datasæt;
proc export data=death_dates outfile="K:\Data\Workdata\702727\FBOI\Data\death_dates.dta" dbms=stata replace;
run;
